import os
import random


trainval_percent = 0.9
train_percent = 0.9
xmlfilepath = r'data\detection\atrw_anno_detection_train\Annotations'
txtsavepath = 'data/detection2/labels'
total_xml = os.listdir(xmlfilepath)

num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)

ftrainval = open(r'datasets\detection2\trainval.txt', 'w')
ftest = open(r'datasets\detection2\test.txt', 'w')
ftrain = open(r'datasets\detection2\train.txt', 'w')
fval = open(r'datasets\detection2\val.txt', 'w')


for i in list:
    # name = total_xml[i][:-4] + '\n'
    file_base, file_extension = os.path.splitext(total_xml[i][:-4])
    file_base+='.jpg\n'
    if i in trainval:
        ftrainval.write(file_base)
        if i in train:
            ftrain.write(file_base)
        else:
            fval.write(file_base)
    else:
        ftest.write(file_base)
    # file_base, file_extension = os.path.splitext(total_xml[i][:-4])
    # file_base+='\n'
    # if i in trainval:
    #     ftrainval.write(file_base)
    #     if i in train:
    #         ftrain.write(file_base)
    #     else:
    #         fval.write(file_base)
    # else:
    #     ftest.write(file_base)

ftrainval.close()
ftrain.close()
fval.close()
ftest.close()
